package com.cc.blog.admin.controller;

import com.cc.blog.admin.domain.dto.*;
import com.cc.blog.admin.service.AdminArticleService;
import com.cc.blog.common.annotation.ApiOperationLog;
import com.cc.blog.common.utils.Result;
import com.cc.blog.web.domain.dto.UpdateArticleIsTopReqDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author cc
 * @Date 2024/7/11 16:30
 */
@RestController
@RequestMapping("/admin/article")
@Api(tags = "Admin 文章模块")
@AllArgsConstructor
public class AdminArticleController {

    private final AdminArticleService articleService;

    @PostMapping("/publish")
    @ApiOperation(value = "文章发布")
    @ApiOperationLog(description = "文章发布")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result publishArticle(@RequestBody @Validated PublishArticleReqDTO publishArticleReqDTO) {
        return articleService.publishArticle(publishArticleReqDTO);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "文章删除")
    @ApiOperationLog(description = "文章删除")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result deleteArticle(@RequestBody @Validated DeleteArticleReqDTO deleteArticleReqDTO) {
        return articleService.deleteArticle(deleteArticleReqDTO);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询文章分页数据")
    @ApiOperationLog(description = "查询文章分页数据")
    public Result findArticlePageList(@RequestBody @Validated FindArticlePageListReqDTO findArticlePageListReqDTO) {
        return articleService.findArticlePageList(findArticlePageListReqDTO);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "查询文章详情")
    @ApiOperationLog(description = "查询文章详情")
    public Result findArticleDetail(@RequestBody @Validated FindArticleDetailReqDTO findArticlePageListReqDTO) {
        return articleService.findArticleDetail(findArticlePageListReqDTO);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新文章")
    @ApiOperationLog(description = "更新文章")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result updateArticle(@RequestBody @Validated UpdateArticleReqDTO updateArticleReqDTO) {
        return articleService.updateArticle(updateArticleReqDTO);
    }

    @PostMapping("/isTop/update")
    @ApiOperation(value = "更新文章置顶状态")
    @ApiOperationLog(description = "更新文章置顶状态")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result updateArticleIsTop(@RequestBody @Validated UpdateArticleIsTopReqDTO updateArticleIsTopReqDTO) {
        return articleService.updateArticleIsTop(updateArticleIsTopReqDTO);
    }
}
